﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Content.Master" Inherits="System.Web.Mvc.ViewPage<MethodWorx.CMS.UI.Models.ConfigurePropertiesModel>" ValidateRequest="false"%>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Properties
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <script language="javascript">








        $(document).ready(function () {
        MethodWorx.CMS.hidePleaseWait();
           $("#properties").accordion({ active: <%=this.ViewData.Model.SelectedPropertyIndex %>, height: 'auto' });
            $("#properties").bind("accordionchange", function (event, ui) {
                $("#SelectedPropertyIndex").val(ui.newContent.attr("index"));
            });
            var buttons = new Array();
            buttons.push({ text: "Save Content", hasText: false, id: "save", icon: "<%=Url.Content("~/images/icons/disk.png")%>" });
            buttons.push({ text: "Refresh", hasText: false, id: "add", icon: "<%=Url.Content("~/images/icons/arrow_refresh.png")%>" });
            buttons.push({ text: 'Edit Template Properties Configuration', hasText: false, id: 'editTemplateProperties', icon: '<%=Url.Content("~/images/icons/table_edit.png")%>' });
            buttons.push({ text: "Reorder Content Up The List", hasText: false, id: "up", icon: "<%=Url.Content("~/images/icons/arrow_up.png")%>" });
            buttons.push({ text: "Reorder Content Down The List", hasText: false, id: "down", icon: "<%=Url.Content("~/images/icons/arrow_down.png")%>" });
            MethodWorx.CMS.Toolbar.setButtons(buttons, "#toolbar");

            $("#save").click(function () {
                save();
            });

            $("#up").click(function()
            {
                //  submit the form
                $("#command").val("up");
                $("form").submit();
            });

            $("#down").click(function()
            {
                //  submit the form
                $("#command").val("down");
                $("form").submit();
            });

            $("#editTemplateProperties").click(function () {
                window.location.href = "<%=Url.Content("~/ConfigureContentTemplate")%>/Properties/<%=this.ViewData.Model.ContentTemplate.Id%>?pageId=<%=this.ViewData.Model.Page.Id%>&contentItemId=<%=this.ViewData.Model.ContentItem.Id %>";
            });

            $(window).resize(resize);

            resize();
        });

        function resize() {
            $("#propertiesContainer").height($(window).height() - 200);

        }
      

        function save() {
            for (var i = 0; i < MethodWorx.CMS.saveProperties.length; i++) {
                MethodWorx.CMS.saveProperties[i]();
            }
            //  submit the form
            $("#command").val("save");
            $("form").submit();

        }
    </script>
  <%using(var form = Html.BeginForm()) {%>
        <h3><%:this.ViewData.Model.Page.Name %> &gt; <%:this.ViewData.Model.ContentItem.Name %> Properties</h3>
        <%=Html.Hidden("Id")%>
        <%=Html.Hidden("command")%>
        <%=Html.Hidden("SelectedPropertyIndex") %>
        <%=Html.Hidden("pageId") %>
        <%=Html.Hidden("contentItemId") %>
        <%if(!this.Model.Properties.Any()) { %>
        <div style="text-align:center">
            <p>No properties exist for this page - To configure the template properties click <img src="<%=Url.Content("~/images/icons/table_edit.png")%>" align="absmiddle"/></p></p>
        </div>
        <% } else { %>
        <%using (Html.BeginForm())
          { %>
                  <div style="text-align:left;">
            <p>Below are the following properties for the content item, To configure the template properties click <img src="<%=Url.Content("~/images/icons/table_edit.png")%>" align="absmiddle"/></p>
        </div>
                <div id="propertiesContainer" style="border:1px solid silver; overflow:auto">
            <div id="properties">
                <%
            foreach (var group in this.Model.Properties.Select(p => p.Group ?? "").Distinct())
            {%>
                    <%
            foreach (var property in this.Model.Properties.Where(p => (p.Group ?? "") == group))
            { %>
             <h3>
                        <a><%=property.Name%></a>
                        </h3>
                        <div>
                        <%=Html.Hidden("PropertyDetails[" + property.Index + "].Index", property.Index)%>
                        <%=Html.Hidden("PropertyDetails[" + property.Index + "].Type", property.Type)%>
                        <%=Html.Hidden("PropertyDetails[" + property.Index + "].Group", property.Group)%>
                        <%=Html.Hidden("PropertyDetails[" + property.Index + "].Name", property.Name)%>
                        <%
            var pc = new MethodWorx.CMS.Configuration.PropertyConfiguration();
            object temp = null;
            MethodWorx.CMS.AssemblyResourceReference arr = null;
            //
            //  from the definition of the type and configuration
            //  load the properties and the editor ascx
            foreach (var p in pc.GetPropertyConfigurations())
            {
                if (p.Name.ToLower() == property.Type.ToLower())
                {
                    temp = Activator.CreateInstance(typeof(MethodWorx.CMS.Plugins.PropertyDetails<>).MakeGenericType(p.GetDetailsType()));
                    arr = p.GetPropertyControl();

                    temp.GetType().InvokeMember("Index", System.Reflection.BindingFlags.SetProperty, null, temp, new object[] { property.Index });
                    temp.GetType().InvokeMember("Name", System.Reflection.BindingFlags.SetProperty, null, temp, new object[] { property.Name });
                    temp.GetType().InvokeMember("Mandatory", System.Reflection.BindingFlags.SetProperty, null, temp, new object[] { property.Mandatory });
                    temp.GetType().InvokeMember("Type", System.Reflection.BindingFlags.SetProperty, null, temp, new object[] { property.Type });
                }
            }
            if (property.Custom != null)
            {
                temp.GetType().InvokeMember("Custom", System.Reflection.BindingFlags.SetProperty, null, temp, new object[] { property.Custom });
            }
                        %>
                        <%if (temp != null) Html.RenderPartial(arr.Name, temp); %>

                        </div>
                <%} %>
                
                <%} %>
           </div>
           </div>
            <%} %>
       <%} %>
    <%}%>

</asp:Content>
